<template>
  <div class="searchs">
    <div class="center">
      <div class="top">
        <div class="search-one">
          <van-icon name="arrow-left" @click="toElemo()"/>
          <h1>搜索页</h1>
        </div>
        <div class="icon">
          <van-icon name="weapp-nav" /><van-icon name="close" />
        </div>
      </div>
      <div class="search-two">
        <input type="text" placeholder="请输入" v-model="searchkey" /><span
          @click="getList"
        >
          搜索</span
        >
      </div>
      <div v-if="!searchkey">
        <div class="search-three">
          <div>历史搜索</div>
          <van-icon name="delete-o" @click="clear" />
        </div>
        <div class="search-four">
          <ul>
            <li v-for="(item, index) in obj" :key="index" v-show="item">
              {{ item }}
            </li>
          </ul>
        </div>
        <div class="search-three">
          <div>搜索发现</div>
        </div>
        <div class="search-four">
          <ul>
            <li v-for="(item, index) in objs" :key="index">{{ item }}</li>
          </ul>
        </div>
      </div>
      <div
        v-for="(item, index) in shopsearch"
        :key="index"
        v-else
        class="shop-list"
        @click="toDetail(item)">
        <img :src="item.picUrl" alt="" />
        <div class="shoplist">
          <h3>{{ item.name }}</h3>
          <span>{{ item.monthSalesTip }}</span>
          <span>{{ item.minPriceTip }}</span>
          <span>配送距离{{ item.distance }}</span>
          <div>{{ item.averagePriceTip }}</div>
        </div>
      </div>
    </div>
  </div>
</template> 

<script>
import { shop_search } from "../utils/api";
export default {
  data() {
    return {
      shopsearch: [],
      searchkey: "",
      obj: [],
      objs: ["麻辣烫", "米线","披萨","蛋糕","汉堡","腊八粥"],
    };
  },
  mounted() {
    // 请求搜索
    this.getList();
  },
  methods: {
    //获取搜索出商品
    getList() {
      if (this.obj.indexOf(this.searchkey) == -1) {
        this.obj.push(this.searchkey);
      }
      shop_search({ searchkey: this.searchkey }).then((res) => {
        this.shopsearch = res.data.list;
        console.log(this.shopsearch);
      });
    },
    //删除历史记录
    clear() {
      this.obj = [];
    },
    //跳转主页
    toElemo(){
        this.$router.go(-1);
    },
    toDetail(item) {
      this.$router.push("/ShopView/" + item.mtWmPoiId);
      console.log(item.mtWmPoiId);
    },
  },
};
</script>

<style scoped lang="scss">
.searchs {
  height: 100%;
  > .center {
    width: 95%;
    height: 7.5rem;
    background-color: #ffffff;
    margin: 0 auto;
    > .top {
      width: 100%;
      display: flex;
      justify-content: space-between;
      height: 3.125rem;
      line-height: 3.125rem;
      > .search-one {
        width: 20%;
        display: flex;
        justify-content: space-between;
        .van-icon-arrow-left {
          font-size: 1.5625rem;
          line-height: 3.125rem;
          color: #0074d9;
        }
      }
      > .icon {
        width: 30%;
        height: 1.875rem;
        font-size: 1.375rem;
        color: #0074d9;
        border: 0.0625rem solid wheat;
        border-radius: 1.875rem;
        align-self: center;
        display: flex;
        justify-content: space-around;
        > .van-icon {
          line-height: 1.875rem;
        }
      }
    }
    .search-two {
      height: 3.125rem;
      line-height: 3.125rem;
      input {
        border-radius: 1.875rem;
        color: black;
        background-color: #f1f1f2;
        height: 2.1875rem;
        width: 80%;
        text-align: center;
      }
      span {
        font-size: 1.125rem;
        font-weight: bold;
        margin-left: 0.625rem;
        color: #0074d9;
      }
    }
    .search-three {
      display: flex;
      justify-content: space-between;
      height: 3.125rem;
      line-height: 3.125rem;
      font-size: 18px;
      font-weight: 700;
      i {
        line-height: 3.125rem;
        margin-right: 0.625rem;
      }
    }
    .search-four {
      ul {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        li {
          background-color: #f1f1f2;
          padding: 0.625rem;
          margin-right: 0.625rem;
          line-height: 1.875rem;
          height: 1.875rem;
          margin-top: .625rem;
        }
      }
    }
    .shop-list {
      width: 90%;
      padding: 0.4375rem;
      display: flex;
      margin: 0 auto;
      > img {
        width: 4.8125rem;
      }
      span {
        color: gray;
        font-size: 1rem;
        margin-right: 0.4375rem;
      }
      > .shoplist {
        margin-left: 0.625rem;
        div {
          color: red;
          border: 0.0625rem red solid;
          width: 25%;
          font-size: 0.875rem;
          margin-top: 0.625rem;
        }
      }
    }
  }
}
h1{
  width:200px;
}
</style>
